import Vue from "vue";
import {request} from "../../../util/request.js";
import dialog from "../../../util/dialog";
import pageing from "../../../components/pageing/pageing";
import "../../../style/basic.less";
new Vue({
    el:"#louie",
    data:{
        returnNum:10,  //请求数据数量
        pageNum:1,  //当前page页面
        total:0,
        list:[],   //列表
        navs:[
            {name:"待提现",value:"0",icon:"iconfont icon-dengdai"},
            {name:"已提现",value:"1",icon:"iconfont icon-chenggong"},
            {name:"拒绝提现",value:"2",icon:"iconfont icon-jujue"}
        ],
        nav_index:0,
        disabled:false,  //操作提现状态
        //批量提现操作
        select_all:false
    },
    components:{
      page:pageing
    },
    created:function(){
        this.getTotal()
            .then(()=>{
                if(this.total > 0){
                    this.getList();
                }
            });
    },
    methods:{
        /**
         * 切换导航
         */
        changeNav:function(index){
            if(this.nav_index !== index){
                this.nav_index = index;
                this.list = [];
                this.pageNum = 1;
                this.getTotal()
                    .then(()=>{
                        if(this.total > 0){
                            this.getList();
                        }
                    });
            }
        },
        /**
         * 获取列表数据总数
         */
        getTotal(){
            return new Promise((success)=>{
                request('cash/getTotal',{
                    status:this.navs[this.nav_index].value
                })
                    .then((res)=>{
                        if(res.data.status === "success"){
                            this.total = parseInt(res.data.data);
                            success();
                        }
                    });
            });
        },
        /**
         * 获取列表
         */
        getList(){
            request("cash/getList",{
                status:this.navs[this.nav_index].value,
                limit:[(this.pageNum-1)*this.returnNum,this.returnNum]
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.list = res.data.data;
                    }else{
                        this.list = [];
                    }
                });
        },
        /**
         * 点击分页
         * @param page
         */
        changePage:function(page){
            this.pageNum = page;
            this.getList();
        },
        /**
         *通过提现
         */
        pass(index){
            dialog.confirm(this,"确定要处理该提现吗？",(res)=>{
                if(res){
                    this.disabled = true;
                    request("cash/changeStatus",{
                        cash_id:this.list[index].id,
                        status:1
                    })
                        .then((res)=>{
                            this.changeStatusSuccess(res);
                        });
                }
            });
        },
        /**
         * 拒绝提现
         */
        refuse(index){
            dialog.confirm(this,"确定拒绝该提现吗？",(res)=>{
                if(res){
                    this.disabled = true;
                    request("cash/changeStatus",{
                        cash_id:this.list[index].id,
                        status:2
                    })
                        .then((res)=>{
                            this.changeStatusSuccess(res);
                        });
                }
            });
        },
        /**
         * 删除提现
         */
        del(index){
            dialog.confirm(this,"确定要删除吗？",(res)=>{
                if(res){
                    this.disabled = true;
                    request("cash/changeStatus",{
                        cash_id:this.list[index].id,
                        status:9
                    })
                        .then((res)=>{
                            this.changeStatusSuccess(res);
                        });
                }
            });
        },
        /**
         * 更改状态成功
         */
        changeStatusSuccess(res){
            if(res.data.status === "success"){
                this.getTotal()
                    .then(()=>{
                        if(this.total > 0){
                            this.getList();
                        }else{
                            this.list = [];
                        }
                    });
            }else{
                dialog.alert(this,res.data.err_msg  ? res.data.err_msg : "操作失败");
            }
            this.disabled = false;
        },
        /**
         * 提现全选
         */
        selectAll(){
            var cash = this.$refs.depositForm.cash;
            if(this.select_all){
                for(var i=0;i<cash.length;i++){
                    cash[i].checked = false;
                }
                this.select_all = false;
            }else{
                for(var i=0;i<cash.length;i++){
                    cash[i].checked = true;
                }
                this.select_all = true;
            }
        },
        /**
         * 批量提现
         */
        sendAllCash(){
            let cash = this.$refs.depositForm.cash;
            let checked = [];
            for(var i=0;i<cash.length;i++){
                if(cash[i].checked){
                    checked.push(cash[i].value);
                }
            }
            if(checked.length <= 0){
                dialog.alert(this,'至少选择一个提现请求');
                return false;
            }
            this.disabled = true;
            request("cash/cashAll",{
                data:checked
            })
                .then((res)=>{
                    if(res.data.status === "success"){
                        this.getTotal()
                            .then(()=>{
                                if(this.total > 0){
                                    this.getList();
                                }else{
                                    this.list = [];
                                }
                            });
                        dialog.alert(this,res.data.data);
                    }else{
                        dialog.alert(this,"批量提现失败");
                    }
                    this.disabled = false;
                });
        }
    }
});